﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model;
using BLL;
using System.Security.Cryptography;
using System.Text;

namespace OA.Controllers
{
    public class MemberAddController : Controller
    {
        //
        // GET: /MemberAdd/

        public ActionResult Index()
        {
         

            return View();
        }
        public ActionResult MemberAdd()
        {
            memberInfoManager memberMer = new memberInfoManager();
            AreasManager areaS = new AreasManager();
            consumeSolutionManager CSManager = new consumeSolutionManager();
            List<consumeSolution> list = CSManager.GetAll().ToList();
          string MaxmemberID=memberMer.GetMaxMemberId();

          if (MaxmemberID == "" || MaxmemberID == string.Empty)
          {
              MaxmemberID = "888101000001";
          }
          else
          {
              MaxmemberID=( Convert.ToInt64(MaxmemberID)+1).ToString();
          }
          string memberCardId = DateTime.Now.ToString("yyyyMMddhhmmssff").Trim();
          ViewData["memberCardId"] = memberCardId;
            List<Areas> li = areaS.GetAll().ToList();
            ViewData["memberId"] = MaxmemberID;
            var selectList = new SelectList(list, "id", "solutionName");
            ViewData["cardType"] = selectList;
            return View(li);
        }

        //添加会员
        public ActionResult Member(FormCollection fc)
        {
            if (System.Web.HttpContext.Current.User.Identity.IsAuthenticated)
           {
                string memberId = fc["memberId"];
            string memberName = fc["memberName"];
            string memberCardId = fc["memberCardId"];
            string index = fc["index"];
            string cardType = fc["cardType"];
            string pwd = fc["pwd"];
            string pwd1 = fc["pwd1"];
            string mobile = fc["mobile"];
            string sex = fc["sex"];
            string email = fc["email"];
            string numberID = fc["numberID"];
            string tel = fc["tel"];
            string birthday = fc["birthday"];
            string status = fc["status"];
            string expiresDate = fc["expiresDate"];
            string createDate = fc["createDate"];
            string marriage = fc["marriage"];
            string areaName = fc["areaName"];
            string zip = fc["zip"];
            string address = fc["address"];
            string memberType = fc["memberType"];
            string tuiguangren = fc["tuiguangren"];
            string memo = fc["memo"];
            bool print = Convert.ToBoolean(fc["print"]);
            if (pwd != pwd1)
            {
                return Content("两次输入密码不一致");
            }
            if(memberName==""||memberName==string.Empty||mobile==""||mobile==string.Empty||birthday==""||birthday==string.Empty || email==""||email==string.Empty)
            {
                return Content("必填项忘填了");
            }
            memberInfo m = new memberInfo();
            memberInfoManager mb = new memberInfoManager();
            m.memberId = memberId;
            m.memberName = memberName;
            m.memberCardId = memberCardId;
            m.cardType = Convert.ToInt32(cardType);
            consumeSolutionManager CSManager = new consumeSolutionManager();
              consumeSolution cs= CSManager.GetByID(Convert.ToInt32(m.cardType));
              string cardTypeName = "";
              if (cs != null)
              {
                 cardTypeName = cs.solutionName;
              }
            if (pwd != "" && pwd != string.Empty)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                byte[] bt = Encoding.Default.GetBytes(pwd);
                bt = md5.ComputeHash(bt);
                m.pwd = BitConverter.ToString(bt).Replace("-", "");
            }
            else
            {
                m.pwd = "未设置";
            }
            
            m.mobile = mobile;
            string sexName = "";
            if (sex == "男")
            {
                m.sex = true;
                sexName = "男";
            }
            else
            {
                m.sex = false;
                sexName = "女";
            }
            m.Email = email;
            if (numberID != "" && numberID != string.Empty)
            {
                m.IDNumber = numberID;
            }
            else
            {
                m.IDNumber= "未设置";
            }
            if (tel != "" && tel != string.Empty)
            {
                m.Tel = tel;
            }
            else
            {
                m.Tel = "未设置";
            }
            if (birthday != null && birthday != string.Empty)
            {
                m.birthDay = DateTime.Parse(birthday);
            }
            else
            {
                m.birthDay =DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            if (expiresDate != null && expiresDate != string.Empty)
            {
                m.expiresDate = DateTime.Parse(expiresDate);
            }
            else
            {
                m.expiresDate = DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            if (createDate != null && createDate != string.Empty)
            {
                m.createDate = DateTime.Parse(createDate);
            }
            else
            {
                m.createDate= DateTime.Parse(DateTime.Now.ToString("yyyy - MM - dd"));

            }
            m.status = int.Parse(status);
            string statusName = "";
            switch (m.status)
            {
                case 0: statusName = "正常";
                    break;
                case 1: statusName = "挂失";
                    break;
                case 2: statusName = "停用";
                    break;
                default: break;
            }
            if (marriage != "" && marriage != string.Empty)
            {
                m.Marriage = marriage;
            }
            else
            {
                m.Marriage = "未设置";
            }
           
            m.AreasID = int.Parse(areaName);
            if (zip != "" && zip != string.Empty)
            {
                m.Zip= zip;
            }
            else
            {
                m.Zip = "未设置";
            }
            if (address != "" && address != string.Empty)
            {
                m.Address= address;
            }
            else
            {
                m.Address = "未设置";
            }
            if (memberType != "" && memberType != string.Empty)
            {
                m.MemberType = memberType;
            }
            else
            {
                m.MemberType = "未设置";
            }
            if (tuiguangren != "" && tuiguangren != string.Empty)
            {
                m.tuiguangren = tuiguangren;
            }
            else
            {
                m.tuiguangren = "未设置";
            }
            if (memo != "" && memo != string.Empty)
            {
                m.memo = memo;
            }
            else
            {
                m.memo = "未设置";
            }
            m.Balance = Convert.ToDecimal(0);
            m.point = 0;
            m.consume = Convert.ToDecimal(0);
            m.shifouhuixing = "未设置";
            try
            {
                if (print)
                {
                    string[] ModelArray = new string[] { m.memberId, cardTypeName, m.memberName, sexName, statusName, m.createDate.ToString(), m.expiresDate.ToString(), "0", "0", "0", "0", "0", "0", "0", "0", "0" };
                    string go = printX(ModelArray,m.cardType);
                    mb.Add(m);
                    mb.Update();
                    wasteBook wb = new wasteBook();
                    wasteBookManager wbManager = new wasteBookManager();
                    wb.memberCardId = m.memberCardId;
                    wb.consumeTimes = 0;
                    string name = System.Web.HttpContext.Current.User.Identity.Name;
                    wb.operatorId = name;
                    wb.consumeType = m.cardType;
                    wb.cardPayMoney = 0;
                    wb.cashPayMoney = 0;
                    wb.consumeMoney =0;
                    wb.consumeIntegral = 0;
                    wb.totalIntegral = 0;
                    wb.exchangeIntegral = 0;
                    wb.savingMoney = 0;
                    wb.refundmentIntegral = 0;
                    wb.refundmentMoney = 0;
                    wb.cardBalance = 0;
                    wb.date = DateTime.Parse(DateTime.Now.ToString());
                   // wb.details = ConfigurationManager.AppSettings["details"];
                    wb.terminalId = 0;
                    wb.jiedairen = m.tuiguangren;
                    wbManager.Add(wb);
                    wbManager.Update();
                    return Content(go);
                }
                mb.Add(m);
                mb.Update();
                wasteBook WB = new wasteBook();
                wasteBookManager WBManager = new wasteBookManager();
                WB.memberCardId = m.memberCardId;
                WB.consumeTimes = 0;
                string nameX = System.Web.HttpContext.Current.User.Identity.Name;
                WB.operatorId = nameX;
                WB.consumeType = m.cardType;
                WB.cardPayMoney = 0;
                WB.cashPayMoney = 0;
                WB.consumeMoney = 0;
                WB.consumeIntegral = 0;
                WB.totalIntegral = 0;
                WB.exchangeIntegral = 0;
                WB.savingMoney = 0;
                WB.refundmentIntegral = 0;
                WB.refundmentMoney = 0;
                WB.cardBalance = 0;
                WB.date = DateTime.Parse(DateTime.Now.ToString());
                // wb.details = ConfigurationManager.AppSettings["details"];
                WB.terminalId = 0;
                WB.jiedairen = m.tuiguangren;
                WBManager.Add(WB);
                WBManager.Update();
                return Content("添加成功");
            }
            catch
            {
                return Content("添加失败");
            }
           }
            else
           {
               return Content("<script>alert('已经超时,请重新登录');window.parent.location.href='/Login/Login';</script>");
            }
            
        }

        public string printX(string[] ModelArray,int cardTypeId)
        {
            cardTemplates ct = new cardTemplates();
            memberInfo m = new memberInfo();
            //Model.memberInfo mo = m.GetUserIdMode(id);

               consumeSolution cs = new consumeSolution();
                    cardTemplates cts = new cardTemplates();
                    consumeSolutionManager csManager = new consumeSolutionManager();
                    cardTemplatesManager ctsManager = new cardTemplatesManager();
                    var csName = csManager.GetByID(cardTypeId);
                    string  solutonName = csName.solutionName;
                    var cardID = csName.cardTemplateId;
                    var card = ctsManager.GetByID(Convert.ToInt32(cardID));
                    string cardTempName = card.template;
         //   Model.memberInfo mode = GetModel(); 
            cardTempName = cardTempName.Replace("\n", "#");
            string[] users = cardTempName.Split('#');
            string muban = "会员卡号#卡片类型#会员姓名#性　　别#卡片状态#办卡日期#截止日期#累计积分#本次积分#可用积分#本次消费#储值余额#折扣金额#本次充值#积分支付#现金消费#空　　行";

            string[] mubanArray = muban.Split('#');

            string[] ModelArray1 = ModelArray;
            string a = users.Length.ToString();

            string t = "";

            for (int i = 0; i < users.Length; i++)
            {

                t += users[i];
                for (int j = 0; j < mubanArray.Length - 1; j++)
                {
                    if (users[i] == mubanArray[j])
                    {
                        t += ":" + ModelArray[j];

                    }

                }
                t += "\n";
            }

            string content = t.Replace("空　　行", "");
            return content;

        }

         
    }
}
